package Controls
{
	import Easing.Circ;
	
	import Objects.GameBitmap;
	import Objects.GameSprite;
	
	import Tools.BitmapTool;
	
	import Tween.ValueTween;
	
	import flash.display.BitmapData;

	public class ProgressBar extends GameSprite
	{
		public var back:GameBitmap;
		public var full:GameBitmap;
		public var label:Label;
		private var fullImage:BitmapData;
		private var valueTween:ValueTween;
		private var percent:Number=0;
		public function ProgressBar()
		{
			super();
			
			back=new GameBitmap();
			addChild(back);
			
			full=new GameBitmap();
			addChild(full);
			
			label=new Label();
			addChild(label);
		}
		private function syncFull(_percent:Number):void
		{
			var imageData:BitmapData;
			var width:int=fullImage.width*_percent;
			if(width>0)
			{
				imageData=BitmapTool.scale(fullImage,width,fullImage.height);
			}
			full.setImage(imageData);
		}
		public function setFull(_percent:Number,ease:Boolean=true):void
		{
			if(percent==_percent)return;
			
			percent=_percent;
			
			if(ease)
			{
				if(valueTween&&!valueTween.removed)
				{
					valueTween.remove();
				}
				valueTween=new ValueTween();
				valueTween.easeing=Circ.easeOut;
				valueTween.startValue=full.obj.width/fullImage.width;
				valueTween.endValue=percent;
				valueTween.duration=100;
				valueTween.updateCallBack=syncFull;
				gs.addPlugin(valueTween);
			}
			else
			{
				syncFull(percent);
			}
		}
		public function setup(width:Number,height:Number,border:int=1):void
		{
			back.scale(width,height);
			full.scale(width-border*2,height-border*2);
			full.obj.x=border;
			full.obj.y=border;
			fullImage=full.bitmap.bitmapData;
			syncFull(0);
		}
		public function setLabel(text:String,html:Boolean=false,handleX:Number=0.5,handleY:Number=0.5):void
		{
			if(html)
			{
				label.textField.htmlText=text;
			}
			else
			{
				label.textField.text=text;
			}
			
			layout(handleX,handleY);
		}
		protected function layout(handleX:Number,handleY:Number):void
		{
			label.moveTo(back.obj.width*handleX,back.obj.height*handleY,0.5,0.5);
		}
	}
}